namespace GomuLibrary.IO.DiscImage.Type
{
    using GomuLibrary;
    using GomuLibrary.IO.DiscImage;
    using System;
    using System.IO;

    public class BinType : IDiscImage
    {
        private string _imagePath;
        private SectorStructure _sector;

        public BinType(string imagePath)
        {
            this._imagePath = imagePath;
            this._sector = new SectorStructure();
        }

        public long FirstDumpSector() => 
            0L;

        public SectorStructure GetSectorStucture()
        {
            this._sector = new SectorStructure();
            FileStream input = null;
            BinaryReader reader = null;
            try
            {
                input = new FileStream(this._imagePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                reader = new BinaryReader(input);
                byte[] buffer = new byte[0x10];
                reader.Read(buffer, 0, buffer.Length);
                if (BytesHelper.CompareBytesArray(BytesHelper.CopyRange(buffer, 0, Iso9660Conv.SYNC_HEADER.Length), Iso9660Conv.SYNC_HEADER))
                {
                    switch (buffer[15])
                    {
                        case 1:
                            this._sector.sectorLength = 0x930;
                            this._sector.userDataOffset = 0x10;
                            goto Label_0111;

                        case 2:
                            this._sector.sectorLength = 0x930;
                            this._sector.userDataOffset = 0x18;
                            goto Label_0111;
                    }
                }
                else
                {
                    this._sector.sectorLength = 0x920;
                    this._sector.userDataOffset = 8;
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (input != null)
                {
                    input.Close();
                    input.Dispose();
                }
            }
        Label_0111:
            return this._sector;
        }

        public string ImagePath =>
            this._imagePath;
    }
}

